package org.gangcai.mac.shop.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.provider.ContactsContract;
import android.util.AttributeSet;
import android.webkit.WebIconDatabase;
import android.widget.ImageView;

import com.blankj.utilcode.util.SizeUtils;

/**
 * Created by mac on 2018/4/1.
 */

public class SquareImageView extends android.support.v7.widget.AppCompatImageView {
    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            setMeasuredDimension(widthSize, widthSize);
        } else {

            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            setMeasuredDimension(heightSize, heightSize);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {

        Path path = new Path();
        RectF rectF = new RectF(0, 0, getWidth(), getHeight());

        int i = SizeUtils.dp2px(8);
        path.addRoundRect(rectF, i, i, Path.Direction.CW);
        canvas.clipPath(path);
        super.onDraw(canvas);


    }
}
